package cn.cttic.base.controller;

import java.util.List;

import cn.cttic.base.api.domain.vo.BaseCreatedUserVo;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.base.api.domain.vo.BasePersonVo;
import cn.cttic.base.api.domain.bo.BasePersonBo;
import cn.cttic.base.service.IBasePersonService;
import cn.cttic.common.mybatis.core.page.TableDataInfo;

/**
 * 人员信息
 * 前端访问路由地址为:/base/person
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/person")
public class BasePersonController extends BaseController {

    private final IBasePersonService basePersonService;

    /**
     * 查询人员信息列表
     */
    @SaCheckPermission("base:person:list")
    @GetMapping("/list")
    public TableDataInfo<BasePersonVo> list(BasePersonBo bo, PageQuery pageQuery) {
        return basePersonService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人员信息列表
     */
    @SaCheckPermission("base:person:export")
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BasePersonBo bo, HttpServletResponse response) {
        List<BasePersonVo> list = basePersonService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员信息", BasePersonVo.class, response);
    }

    /**
     * 获取人员信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:person:query")
    @GetMapping("/{id}")
    public R<BasePersonVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable String id) {
        return R.ok(basePersonService.queryById(id));
    }

    /**
     * 获取人员信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:person:edit")
    @PostMapping("/createSaleUser/{id}")
    public R<BaseCreatedUserVo> createSaleUser(@NotNull(message = "主键不能为空")
                                                  @PathVariable String id) {
        return R.ok(basePersonService.createSaleUser(id));
    }

    /**
     * 获取人员信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("base:person:edit")
    @PostMapping("/createInstallUser/{id}")
    public R<BaseCreatedUserVo> createInstallUser(@NotNull(message = "主键不能为空")
                                   @PathVariable String id) {
        return R.ok(basePersonService.createInstallUser(id));
    }

    /**
     * 新增人员信息
     */
    @SaCheckPermission("base:person:add")
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(basePersonService.insertByBo(bo));
    }

    /**
     * 修改人员信息
     */
    @SaCheckPermission("base:person:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(basePersonService.updateByBo(bo));
    }

    /**
     * 修改人员信息
     */
    @SaCheckPermission("base:person:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/toggleStatus")
    public R<Void> toggleStatus(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        if (bo.getStatus() == 1) {
            return toAjax(basePersonService.disablePerson(bo.getId()));
        } else {
            return toAjax(basePersonService.enablePerson(bo.getId()));
        }
    }



    /**
     * 修改人员信息
     */
    @SaCheckPermission("base:person:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> disable(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(basePersonService.disableByBo(bo));
    }

    /**
     * 修改人员信息
     */
    @SaCheckPermission("base:person:edit")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> enable(@Validated(EditGroup.class) @RequestBody BasePersonBo bo) {
        return toAjax(basePersonService.enableByBo(bo));
    }


    /**
     * 删除人员信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("base:person:remove")
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(basePersonService.deleteWithValidByIds(List.of(ids), true));
    }
}
